﻿/*
____
 L|ypps Web controls for ASP.NET

Copyright (c) 2009 Alessandro Zifiglio, http://www.Typps.com
EMail: alessandro@Typps.com

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
namespace Typps.Tests
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    using Typps;

    /// <summary>
    /// Summary description for FixerUnit
    /// </summary>
    [TestClass]
    public class UnitTestMSWordParser
    {
        #region Fields

        private TestContext testContextInstance;

        #endregion Fields

        #region Constructors

        public UnitTestMSWordParser()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #endregion Properties

        #region Methods

        [TestMethod]
        public void Cleanup_AppendAttributeToExistingStyleAttribute_ReplacedWithCSSVariants()
        {
            MSWordParser parser = new MSWordParser();
            string html = "<p width=\"300px\" style=\"margin:0px\">When I wanted sunshine, I got rain!</p>";

            html = parser.Cleanup(html);
            string output = "<p style=\"margin:0px;width:300px;\">When I wanted sunshine, I got rain!</p>";
            Assert.AreEqual(html, output);
        }

        [TestMethod]
        public void Cleanup_CustomInlineXmlOElement_Removed()
        {
            MSWordParser parser = new MSWordParser();
            //Note <o:p />
            string html =
            @"<p class=""MsoNormal"">
            <b><span style=""mso-ansi-language: EN-US"" lang=""EN-US"">Hello</span></b>
            <span style=""mso-ansi-language: EN-US"" lang=""EN-US"">, yes ok whatever<o:p /></span>
            </p>";
            html = parser.Cleanup(html);
            string output = "<p>\r\n            <b>Hello</b>\r\n            , yes ok whatever\r\n            </p>";
            Assert.AreEqual(html, output);
        }

        [TestMethod]
        public void Cleanup_FaceAttribute_ReplacedWithCSSFontFamily()
        {
            MSWordParser parser = new MSWordParser();
            string html = "<font face=\"verdana\">This is some text!</font>";
            html = parser.Cleanup(html);
            string output = "<span style=\"font-family:verdana;\">This is some text!</span>";
            Assert.AreEqual(html, output);
        }

        [TestMethod]
        public void Cleanup_Font_ReplacedWithSpanAndCSS()
        {
            MSWordParser parser = new MSWordParser();
            string html = "<font size=\"2\">Hey man</font>";
            html = parser.Cleanup(html);
            string output = "<span style=\"font-size:x-small;\">Hey man</span>";
            Assert.AreEqual(html, output);
        }

        [TestMethod]
        public void Cleanup_GTEMSOConditionalComment_Removed()
        {
            MSWordParser parser = new MSWordParser();
            string html =
            @"<!--[if gte mso 9]>
            <xml>
              <w:WordDocument>
            <w:View>Normal</w:View>
              </w:WordDocument>
            </xml>
            <![endif]-->";
            html = parser.Cleanup(html);
            string output = string.Empty;
            Assert.AreEqual(html, output);

            //missing comment ending tag
            html = "<!--[if gte mso 9]><p>Hello</p>";
            html = parser.Cleanup(html);
            output = "<p>Hello</p>";
            Assert.AreEqual(html, output);

            //missing comment starting tag closing angle and ending tag
            html = "<!--[if gte mso 9]<p>Hello</p>";
            html = parser.Cleanup(html);
            output = "<p>Hello</p>";
            Assert.AreEqual(html, output);
        }

        [TestMethod]
        public void Cleanup_HeightAttribute_ReplacedWithCSSHeight()
        {
            MSWordParser parser = new MSWordParser();
            string html = "<img height=400 src=\"Images/test.gif\">";
            html = parser.Cleanup(html);
            string output = "<img src=\"Images/test.gif\" style=\"height:400px;\">";
            Assert.AreEqual(html, output);
        }

        [TestMethod]
        public void Cleanup_HSpaceAttribute_ReplacedWithCSSPadding()
        {
            MSWordParser parser = new MSWordParser();
            string html = "<img src=\"Images/test.gif\" hspace=12>";
            html = parser.Cleanup(html);
            string output = "<img src=\"Images/test.gif\" style=\"padding-left:12px;padding-right:12px;\">";
            Assert.AreEqual(html, output);
        }

        [TestMethod]
        public void Cleanup_LinkTag_Removed()
        {
            MSWordParser parser = new MSWordParser();
            //usage of link tag in the body is not allowed
            string html =@"<link href=""file:///C:%5CUsers%5Cusername%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx"" rel=""themeData"" />";
            html = parser.Cleanup(html);
            string output = string.Empty;
            Assert.AreEqual(html, output);
        }

        [TestMethod]
        public void Cleanup_MetaTag_Removed()
        {
            MSWordParser parser = new MSWordParser();
            //usage of meta tag in the body is not allowed
            string html = @"<meta content=""Microsoft Word 14"" name=""Originator"" />";
            html = parser.Cleanup(html);
            string output = string.Empty;
            Assert.AreEqual(html, output);
        }

        /*
         feature is deprecated
        [TestMethod]
        public void Cleanup_MSONormalClassAttribute_ReplaceWithInlineCSS()
        {
            MSWordParser parser = new MSWordParser();
            string html = "<style type=\"text/css\">.MsoNormal{color:#000;}</style><p class=\"MsoNormal\">weiiiii</p>";
            html = parser.Cleanup(html);
            string output = "<p style=\"color:#000\">weiiiii</p>";
            Assert.AreEqual(html, output);
        }*/
        /*
         feature is deprecated
        [TestMethod]
        public void Cleanup_MultipleClassNameInStyleTag_ReplaceWithInlineCSSEquivalent()
        {
            MSWordParser parser = new MSWordParser();
            string html = "<style>p.MsoNormal, li.MsoNormal, div.MsoNormal{color:#000;}</style><p class=\"MsoNormal\">hello?</p>";

            html = parser.Cleanup(html);
            string output = "<p style=\"color:#000\">hello?</p>";
            Assert.AreEqual(html, output);
        }*/
        [TestMethod]
        public void Cleanup_MSOStyleAttributeInlineCSS_Removed()
        {
            MSWordParser parser = new MSWordParser();

            string html = "<div style=\"border-color: windowtext windowtext windowtext -mso-use-text-color\">hello</div>";
            html = parser.Cleanup(html);
            string output = "<div>hello</div>";
            Assert.AreEqual(html, output);

            html = "<div style=\"background:#000;border-color: windowtext windowtext windowtext -mso-use-text-color\">hello</div>";
            html = parser.Cleanup(html);
            output = "<div style=\"background:#000;\">hello</div>";
            Assert.AreEqual(html, output);
        }

        [TestMethod]
        public void Cleanup_SizeAttribute_ReplacedWithCSSFontSize()
        {
            MSWordParser parser = new MSWordParser();
            string html = "<font size=\"5\">Hey man</font>";
            html = parser.Cleanup(html);
            string output = "<span style=\"font-size:medium;\">Hey man</span>";
            Assert.AreEqual(html, output);
        }

        [TestMethod]
        public void Cleanup_SpanLangAttribute_Removed()
        {
            MSWordParser parser = new MSWordParser();
            string html =
            @"<b><span style=""mso-ansi-language: EN-US"" lang=""EN-US"">Hello</span></b>";
            html = parser.Cleanup(html);
            string output = "<b>Hello</b>";
            Assert.AreEqual(html, output);

            html = @"<p class=""MsoNormal""><span lang=""EN-US"" style=""mso-ansi-language:EN-US"">Best regards,</span></p>";
            html = parser.Cleanup(html);
            output = "<p>Best regards,</p>";
            Assert.AreEqual(html, output);

            html = @"<p class=""MsoNormal""><span lang=""EN-US"" style=""color:#000;"">Best regards,</span></p>";
            html = parser.Cleanup(html);
            output = "<p>Best regards,</p>";
            Assert.AreEqual(html, output);

            html = @"<span style=""color:#000;"" lang=""EN-US"">Best regards,</span>";
            html = parser.Cleanup(html);
            output = "<span style=\"color:#000;\">Best regards,</span>";
            Assert.AreEqual(html, output);
        }

        [TestMethod]
        public void Cleanup_SpanSpace_Removed()
        {
            /*<p class="MsoNormal"><span lang="EN-US" style="mso-ansi-language:EN-US">requirements of your applications. <span style="mso-spacerun:yes"> </span></span></p>*/
            MSWordParser parser = new MSWordParser();
            string html =
            @"<p class=""MsoNormal""><span lang=""EN-US"" style=""mso-ansi-language:EN-US"">requirements of your applications. <span style=""mso-spacerun:yes""> </span></span></p>";
            html = parser.Cleanup(html);
            string output = "<p>requirements of your applications.  </p>";
            Assert.AreEqual(html, output);
        }

        [TestMethod]
        public void Cleanup_StyleBlock_Removed()
        {
            MSWordParser parser = new MSWordParser();
            string html = "<style><!-- /* Font Definitions */ @font-face{font-family:Cambria;mso-generic-font-family:roman;mso-fontpitch:variable}--></style>";
            html = parser.Cleanup(html);
            string output = string.Empty;
            Assert.AreEqual(html, output);
        }

        [TestMethod]
        public void Cleanup_VML_ReplacedWithEmbeddedContent()
        {
            MSWordParser parser = new MSWordParser();
            string html =
            @"<![if !vml]>
            <span style=""z-index: 5; left: 1px; width: 758px; position: absolute; top: 2px; height: 104px"">
            <img alt="""" height=""104"" width=""758"" src=""image322.png"" v:shapes=""_x0000_s1037"">
            </span>
            <![endif]>

            <!--[if !vml]-->
            <span style=""z-index: 5; left: 1px; width: 758px; position: absolute; top: 2px; height: 104px"">
            <img alt="""" height=""104"" width=""758"" src=""image322.png"" v:shapes=""_x0000_s1037"">
            </span>
            <!--[endif]-->";
            html = parser.Cleanup(html);
            string output = "\r\n            <span style=\"z-index: 5; left: 1px; width: 758px; position: absolute; top: 2px; height: 104px\">\r\n            <img alt=\"\" src=\"image322.png\" style=\"height:104px;width:758px;\">\r\n            </span>\r\n            \r\n\r\n            \r\n            <span style=\"z-index: 5; left: 1px; width: 758px; position: absolute; top: 2px; height: 104px\">\r\n            <img alt=\"\" src=\"image322.png\" style=\"height:104px;width:758px;\">\r\n            </span>\r\n            ";
            Assert.AreEqual(html, output);
        }

        [TestMethod]
        public void Cleanup_VShapesAttribute_Removed()
        {
            MSWordParser parser = new MSWordParser();
            string html = "<img width=492 height=400 src=\"Images/test.gif\" align=left v:shapes=\"_x0000_s1027\">";
            html = parser.Cleanup(html);
            string output = "<img src=\"Images/test.gif\" align=left style=\"width:492px;height:400px;\">";
            Assert.AreEqual(html, output);
        }

        [TestMethod]
        public void Cleanup_VSpaceAttribute_ReplacedWithCSSPadding()
        {
            MSWordParser parser = new MSWordParser();
            string html = "<img src=\"Images/test.gif\" vspace=12>";
            html = parser.Cleanup(html);
            string output = "<img src=\"Images/test.gif\" style=\"padding-top:12px;padding-bottom:12px;\">";
            Assert.AreEqual(html, output);
        }

        [TestMethod]
        public void Cleanup_WidthAttribute_ReplacedWithCSSWidth()
        {
            MSWordParser parser = new MSWordParser();
            string html = "<img width=492 src=\"Images/test.gif\">";
            html = parser.Cleanup(html);
            string output = "<img src=\"Images/test.gif\" style=\"width:492px;\">";
            Assert.AreEqual(html, output);
        }

        #endregion Methods

        #region Other

        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //

        #endregion Other
    }
}